import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.google.common.collect.Lists.newArrayList;

public class FootBall {

    private List<Player> players;
    private MenuCommand menuCommand;
    private Map<String, MenuCommand> commandsList;
    private PrintStream printStream;
    private BufferedReader bufferedReader;
    private String userInput;

    public FootBall(List<Player> players, Map<String, MenuCommand> commandsList, PrintStream printStream, BufferedReader bufferedReader) {
        this.players = players;
        this.commandsList = commandsList;
        this.printStream = printStream;
        this.bufferedReader = bufferedReader;
        this.userInput = null;
    }


    public static void main(String[] args) {

        Map<String, MenuCommand> commandsList = new HashMap<String, MenuCommand>(2);
        PrintStream printStream = new PrintStream(System.out);
        List<Player> players = newArrayList(new Player("Bill", new Team("Bears"), 12, 35), new Player("Jeff", new Team("Bears"), 11, 32));
        commandsList.put("1",new ListPlayersCommand(printStream, players));
        commandsList.put("2",new FindPlayerCommand());
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        new FootBall(players, commandsList, System.out, bufferedReader).run();
    }

    public void run() {

        displayMenu();
        try {
        userInput = retrieveUserInputForMenuOption();

        } catch (IOException ex) {
            System.out.println("Problems with System.in");
        }

        executeCommand(userInput);
    }



    public void executeCommand(String userInputString) {
            commandsList.get(userInputString).act();
    }

    public String retrieveUserInputForMenuOption() throws IOException {
        return bufferedReader.readLine();
    }

    public void displayMenu() {
        System.out.println("Choose and option");
        for(MenuCommand menuCommand : commandsList.values()) {
            printStream.println( menuCommand.toString());
        }
    }
}
